<template>
  <div>
    <div>
      <div class="header flex">
        <el-input v-model="user" placeholder="用户名"></el-input>
        <el-button @click="search()" type="primary" icon="el-icon-zoom-in">查询</el-button>
        <el-button @click="cleartIn()" type="primary" icon="el-icon-refresh-left">重置</el-button>
        <el-button type="primary" @click="add()" icon="el-icon-plus">新增</el-button>
        <el-button type="danger" @click="clear()"  icon="el-icon-delete">删除</el-button>
        <!-- 弹框  上班-->
        <el-dialog title="新增员工" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="登录账号" :label-width="formLabelWidth">
      <el-input v-model="form.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" :label-width="formLabelWidth">
      <el-input v-model="form.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="form.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="所属部门" :label-width="formLabelWidth">
      <el-select v-model="form.value" placeholder="请选择">
    <el-option
      v-for="item in form.options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="角色" :label-width="formLabelWidth">
      <el-radio-group v-model="form.radio">
    <el-radio label="管理员">管理员</el-radio>
    <el-radio label="财务专员">财务专员</el-radio>
    <el-radio label="人事专员">人事专员</el-radio>
    <el-radio label="普通员工">普通员工</el-radio>
  </el-radio-group>
    </el-form-item>
    <el-form-item label="状态" :label-width="formLabelWidth">
      <el-radio-group v-model="form.radio2">
    <el-radio label="禁用">禁用</el-radio>
    <el-radio label="正常">正常</el-radio>
  </el-radio-group>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm(form)">确 定</el-button>

  </div>
</el-dialog>
    <!-- 弹框  下班-->
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible2">
  <el-form :model="form2">
    <el-form-item label="登录账号" :label-width="formLabelWidth">
      <el-input v-model="form2.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" :label-width="formLabelWidth">
      <el-input v-model="form2.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="姓名" :label-width="formLabelWidth">
      <el-input v-model="form2.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="所属部门" :label-width="formLabelWidth">
      <el-select v-model="form2.value" placeholder="请选择">
    <el-option
      v-for="item in form2.options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="角色" :label-width="formLabelWidth">
      <el-radio-group v-model="form2.radio">
    <el-radio label="管理员">管理员</el-radio>
    <el-radio label="财务专员">财务专员</el-radio>
    <el-radio label="人事专员">人事专员</el-radio>
    <el-radio label="普通员工">普通员工</el-radio>
  </el-radio-group>
    </el-form-item>
    <el-form-item label="状态" :label-width="formLabelWidth">
      <el-radio-group v-model="form2.radio2">
    <el-radio label="禁用">禁用</el-radio>
    <el-radio label="正常">正常</el-radio>
  </el-radio-group>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="submitForm2(form2)">确 定</el-button>
  </div>
</el-dialog>
      </div>
      <!-- 下面表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        border
        @selection-change="handleSelectionChange">
      <!-- 单选全选 点击按钮 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="登录账号" width="180">
          <template slot-scope="scope">{{ scope.row.username }}</template>
        </el-table-column>
        <el-table-column prop="name" label="姓名" width="180">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column prop="address" label="所属部门" width="180">
          <template slot-scope="scope">{{ scope.row.deptName }}</template>
        </el-table-column>
        <el-table-column prop="address" label="职位" width="180">
          <template slot-scope="scope">{{ scope.row.roleNote  }}</template>
        </el-table-column>
        <el-table-column label="状态" width="180">
          <template slot-scope="scope">
            <el-tag size="50" :type="scope.row.types" effect="dark">{{ scope.row.type }}</el-tag>
      </template>
    </el-table-column>
        <el-table-column prop="address" label="创建时间" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.createDate }}</template>
        </el-table-column>
        <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="60"
          @click="handleEdit(scope.row)" type="warning" icon="el-icon-edit">修改</el-button>
      </template>
      </el-table-column>
      </el-table>
      <Pagin :count="total" @pageEvent="fetchData" :pageSize="pageSize"></Pagin>
    </div>
  </div>
</template>
<script>
import {staListAPI,addStaInsertAPI,staUpdateAPI,staDeleteAPI,staSeach,depListAPI} from '../../api/sta'
export default {
  data() {
    return {
      date:'',
      time:'',
      form: {
          username: '',
          name: '',
          password: '',
          options:[],
          value : '',
          radio : '',
          radio2 : ''
        },
      formLabelWidth: '120px',
      dialogFormVisible: false,
      dialogFormVisible2: false,//修改
      form2: {
          username: '',
          name: '',
          password: '',
          options:[],
          value : '',
          radio : '',
          radio2 : ''
        },
      dataList:[],
      total: 0, // 列表总行数
      currentPage: 1,
      pageSize: 10,
      tableData: [],
      multipleSelection: [],
      user:''
    };
  },
  created(){
    this.jia()
    this.bumen()
  },
  components: {
    Pagin: () => import('c/Pagin')
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //重置
    cleartIn(){
      this.user =''
    },
    //查询
   async search(){
    const res = await staSeach({
      name : this.user
    })
    if(res.code == '1'){
        if(res.data != null){
          this.dataList = res.data
          this.total = this.dataList.length
          this.fetchData(this.currentPage)
          this.$message.success(res.msg)
        }
      }else{
          this.$message.error(res.msg)
        }
    },
     //修改
     handleEdit(row){
      console.log(row,'修改');
      this.dialogFormVisible2 = true
      this.form2.username  = row.username
      this.form2.password  = row.password
      this.form2.name  = row.name
      this.form2.id = row.id
      this.form2.value = row.deptName
      this.form2.radio = row.roleNote
      this.form2.radio2 = row.type
    },
    //修改确定
   async submitForm2(form){
      this.dialogFormVisible2 = false
      const res  = await staUpdateAPI({
        username : form.username, //主题名
        name :  form.name,//开始时间
        password :  form.password,//内容
        deptName  : form.value,
        roleNote  : form.radio,
        type  : form.radio2,
        id : form.id
      })
      if(res.code == '1'){
        this.jia()
        this.$message.success('修改成功')
       }else{
        this.$message.error('修改失败')
       }
    },
   async submitForm(form) {
    const res = await addStaInsertAPI({
        username : form.username, //主题名
        name :  form.name,//开始时间
        password :  form.password,//内容
        deptName  : form.value,
        roleNote  : form.radio,
        type  : form.radio2,
    })
      //请求成功后
      if(res.code == '1'){
        this.jia()
        this.$message.success('添加成功')
       }else{
        this.$message.error('添加失败')
       }
      //关闭
      this.dialogFormVisible = false
    },
    //删除
   async clear(){
      var id = []
      this.multipleSelection.forEach(item=>{
         id.push(item.id)
      })
      if(id.length > 0){
        const res = await staDeleteAPI(id)
         //请求成功后
         if(res.code == '1'){
        this.jia()
        this.$message.success('删除成功')
       }else{
        this.$message.error('删除失败')
       }
      }
    },
      //新增
      add(){
      this.dialogFormVisible = true
    },
    async bumen(){
      const res = await depListAPI()
      // console.log(res2);
      var arr = []
      res.data.forEach(item=>{
        var obj = {value:item,label:item}
        arr.push(obj)

      })
      this.form.options = arr
      this.form2.options = arr
    },
    //模拟假数据
   async jia(){
    const res = await staListAPI()
    res.data.map(item=>{
      if(item.type == '禁用'){
          item.types = 'danger'
         }else{
          item.types = 'success'
         }
    })
      this.dataList = res.data
      this.total = this.dataList.length
      this.fetchData(this.currentPage)
    },
    fetchData (count) {
      var list = this.dataList
      var from = (count - 1) * this.pageSize
      const to = count * this.pageSize
      this.tableData = []
      for (; from < to; from++) {
        if (list[from]) {
          this.tableData.push(list[from])
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.input {
  width: 120px;
}
.header {
  width: 50%;
  // border-bottom: 1px solid #e0e0e0;
  padding: 20px 0;
}

</style>
